package history.hot100;

import java.util.Deque;
import java.util.LinkedList;

// 739. 每日温度:https://leetcode-cn.com/problems/daily-temperatures/
// https://leetcode-cn.com/problems/daily-temperatures/solution/mei-ri-wen-du-by-leetcode-solution/
public class LeetCode_739 {
    public int[] dailyTemperatures(int[] temperatures) {
        int length = temperatures.length;
        int []ans = new int[length];
        Deque<Integer> stack = new LinkedList<>();
        for (int i = 0; i < length; i++) {
            int temp = temperatures[i];
            while (!stack.isEmpty()&& temp > temperatures[stack.peek()]) {
                int prevIndex = stack.pop();
                ans[prevIndex] = i - prevIndex;
            }
            stack.push(i);
        }
        return ans;
    }
}
